package org.dhbw.jee.business.db;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

import org.dhbw.jee.persistence.dataAccess.DataAccessObject;
import org.dhbw.jee.web.security.Hash;

/**
 * The persistent class for the user database table.
 * 
 */
@Entity(name = "USER")
@NamedQueries({//
@NamedQuery(name = User.GETALL, query = "SELECT u FROM USER u"),//
@NamedQuery(name = User.BY_ID, query = "SELECT u FROM USER u where u.id = ?"),//
@NamedQuery(name = User.BY_NICK, query = "SELECT u FROM USER u where u.nickName = ?"),//
@NamedQuery(name = User.BY_NICK_AND_PASS, query = "SELECT u FROM USER u where u.nickName = ? AND u.password = ?") //
})
public class User implements Serializable {
	private static final long serialVersionUID = 1L;

	public static final String GETALL = "User.findAll";
	public static final String BY_ID = "User.byId";
	public static final String BY_NICK = "User.byNick";
	public static final String BY_NICK_AND_PASS = "User.byNickAndPass";

	@Inject
	DataAccessObject dao;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "ID")
	private int id;

	@Column(name = "NICKNAME")
	private String nickName;

	@Column(name = "PASSWORD")
	private String password;

	@Column(name = "EMAIL")
	private String email;

	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "ALBUMCOLLECTION", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "ALBUM_ID", referencedColumnName = "ID") })
	private List<Album> albums;

	@ManyToOne
	private Role role;

	public User() {
		super();
	}

	public User(String nickName, String password, String email, Role role) {
		this();
		this.nickName = nickName;
		this.password = password;
		this.email = email;
		this.role = role;
	}

	@PostConstruct
	public void postConstruct() {
		System.out.println("Post");
		System.out.println("Albums.size() -> " + albums.size());
	}

	@PreDestroy
	public void preDestroy() {
		this.albums.clear();
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = Hash.toMD5(password);
	}

	public List<Album> getAlbums() {
		return this.albums;
	}

	public void setAlbums(List<Album> albums) {
		this.albums = albums;
	}

	public Role getRole() {

		return this.role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public int getId() {
		return id;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	@Override
	public String toString() {
		return new StringBuilder().append("User (").append(id).append(", ").append(nickName).append(")").toString();
	}
}